package api_servers
{
	import flash.net.URLRequest;
	
	public class pi_myserver extends api_myserver {
		public static var ROOT_URL: String = '';
//		public static var MODEL_NAME: String = 'pj_dev';
		public static var MODEL_NAME: String = 'pj08';

		public function pi_myserver(app : photo_inject, myServerURL: String, api_id: String='441805', secret: String='3b0c9d411eb5c5dc252c9fe26d4da53a') {
			super(app, myServerURL, api_id, secret);
			ROOT_URL = pi_servers.ADDR[0];
		}
		
		protected function get app(): photo_inject {
			return sroot as photo_inject
		}
		
		public function get modelName(): String {
			return MODEL_NAME;
		} 
		
		public function sendCard(uid: String, users: Array, 
								cardParams: String, 
								userName: String = '', 
								saveOutBox: uint=1,
								_handle: Function = null): void {
			var params: Array = [
				{pn:'querycount', pv: users.length}
			];
			
			for (var i:int = 1; i<=users.length; i++) {
				params.push({
					pn:'query' + i,
					pv:MODEL_NAME + ';sendCard;' + uid + ';' + users[i - 1].uid + ';' + cardParams + ';' + 
									userName + ';' + saveOutBox
				})
			}
			jsrHelper(params, _handle, 'sendCard');
		}
		
		public function oneQuery(method: String, str_params: String='', _handle: Function=null, a_cache: Boolean = false, a_url: String = ''): void {
			var params: Array = [
				{pn:'querycount', pv: 1},
				{pn:'query1', pv: MODEL_NAME + ';' + method + ';' + str_params}
			];

			jsrHelper(params, _handle, method, a_cache, a_url);
		}
		
	    public function getInBox(uid: String, handle_: Function = null): void {
	    	oneQuery('getInBox', uid, handle_);
	    }
	    
	    public function getOutBox(uid: String, handle_: Function = null): void {
	    	oneQuery('getOutBox', uid, handle_);
	    }
	    
	    public function getBalance(uid: String, handle_: Function = null): void {
			var params: Array = [
				{pn:'querycount', pv: 1},
				{pn:'query1', pv: MODEL_NAME + ';getBalance;' + uid}
			];

			jsrHelper(params, handle_, 'balance_request');
	    }
	    
	    public function setTransaction(uid: String, serviceID: int, sum: Number, 
	    				a_params: String, handle_: Function=null): void {
			var params: Array = [
				{pn:'querycount', pv: 1},
				{pn:'query1', pv: MODEL_NAME + ';setTransaction;' + uid + ';' + serviceID + ';' + sum + ';' + a_params}
			];

			jsrHelper(params, handle_, 'transaction_request');
	    }
	    
	    public function getStartData(uid: String, 
	    							fInfo: Function, 
	    							BalanceFunc: Function,
	    							prepaidFunc: Function
	    							): void {
			var params: Array = [
				{pn:'querycount', pv: 3},
				{pn:'query1', pv: MODEL_NAME + ';getInfo;' + uid},
				{pn:'query2', pv: MODEL_NAME + ';getBalance;' + uid},
				{pn:'query3', pv: MODEL_NAME + ';getPrepaid;' + uid + ';24'}
			];

			jsrHelper(params, function(data: *): void {
				if (data.response[0]) fInfo(data.response[0]);
				if (data.response[1]) BalanceFunc(data.response[1]);
				if (data.response[2]) prepaidFunc(data.response[2]);
			}, pi_data.START_REQUEST);
	    }
	    
/*	    public function receiveCardViewed(send_id: uint): void {
			var params: Array = [
				{pn:'querycount', pv: 1},
				{pn:'query1', pv: MODEL_NAME + ';receiveCardViewed;' + send_id}
			];

			jsrHelper(params, null, 'receiveCardViewed');
	    }*/
	    
	    public function receiveCardViewedAsUser(cardID: String, viewer_id: String,  _handle: Function=null): void {
	    	oneQuery('receiveCardViewedAsUser', cardID + ';' + viewer_id, _handle);
	    }
	    
	    public function collectorImage(a_params: Array, _handle: Function, request: URLRequest =null): void {
			jsrHelper(a_params, _handle, 'collectorImage', false, '', request);
	    }
	    
	    public function checkPrepaid(a_params: Array, _handle: Function, request: URLRequest =null): void {
			jsrHelper(a_params, _handle, 'checkPrepaid', false, '', request);
	    }
	    
/*	    public function checkPrepaid(uid: String, _lastCollectFileTime: uint, _handle: Function): void {
	    	oneQuery('checkPrepaid', uid + ';null;' + _lastCollectFileTime, _handle);
	    }*/ 

	    public function addNoPayCollector(uid: String, cardID: int): void {
	    	oneQuery('addNoPayCollector', uid + ';' + cardID, null);
	    }
	    
	    public function deleteCard(send_id: int, _handle: Function): void {
	    	oneQuery('deleteCard', send_id.toString(), _handle);
	    }
	    
	    public function getPrepaid(uid: String, _handle: Function): void {
			var params: Array = [
				{pn:'querycount', pv: 1},
				{pn:'query1', pv: MODEL_NAME + ';getPrepaid;' + uid + ';24'}
			];

			jsrHelper(params, _handle, 'getPrepaid');
	    } 
	   
	    public function getCard(viewer_id: String, send_id: String, _handle: Function): void {
	    	oneQuery('getCard', viewer_id + ';' + send_id, _handle);
	    }
	    
/*	    public function getGroupCards(groupID: String, _handle: Function): void {
	    	oneQuery('getGroupCards', groupID, _handle);
	    }*/
	    
	    public function removeImage(fileURL: String): void {
	    	pi_servers.UPLOADJPG_URL
			var params: Array = [
				{pn:'querycount', pv: 1},
				{pn:'query1', pv: MODEL_NAME + ';removeImage;' + fileURL}
			];

			jsrHelper(params, null, 'removeImage', false, pi_servers.UPLOADJPG_URL);
	    }
	    
	    public function stat(model: String, a_params: String): void {
			var params: Array = [
				{pn:'querycount', pv: 1},
				{pn:'query1', pv: 'stat;' + model + ';' + a_params}
			];

			jsrHelper(params, null, 'stat' + model);
	    }
	    
	    public function successStart(uid: String, fp_v: int): void {
	    	stat('modifyProfile', uid + ';' + fp_v);
	    }	    
	}
}